package com.tangchao.client.sdh.view.card
{
	import com.tangchao.client.sdh.definition.DFGetCard;
	import com.tangchao.client.sdh.sdhinterface.Out;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	public class CardCell extends MovieClip
	{
		private var _index:int;
		private var _mc:MovieClip;
		private var _select:Boolean=false;
		
		private var _parentUI:CardGrid;
		
		public function CardCell()
		{
			
		}
		
		public function set parentUI(ui:CardGrid):void
		{
			_parentUI = ui;
		}
		
		public function set index(i:int)
		{
			_index=i;
			_mc=DFGetCard.getCardByIndex(i);
			if(_mc)
			{
				addChild(_mc);
				_mc.stop();
				_mc.addEventListener(MouseEvent.CLICK,onCardClick);
			}
		}
		
		private function onCardClick(e:MouseEvent)
		{
			if(_mc.currentFrame==1)
			{
				_mc.gotoAndStop(2);
				_select=true;
			}
			else if(_mc.currentFrame==2)
			{
				_mc.gotoAndStop(1);
				_select=false;
			}
			
			_select = !select;
			
			if(_parentUI)
			{
				_parentUI.onCilckCard(_select);
			}
		}
		
		public function get select():Boolean
		{
			return _select;
		}
		
		public function set select(b:Boolean)
		{
			_select=b;
			if(_mc._select)
			{
				_mc.gotoAndStop(2);
			}
			else
			{
				_mc.gotoAndStop(1);
			}
		}
		
		public function reset()
		{
			if(_mc)
			{
				_mc.gotoAndStop(1);
			}
		}
		
		public function get index():int
		{
			return _index;
		}
		
		public function destroy()
		{
			if(_mc && this.contains(_mc))
			{
				_mc.removeEventListener(MouseEvent.CLICK,onCardClick);
				removeChild(_mc);
				_mc=null;
			}
		}
	}
}